Displaying expressions with the help of the "Text" object

One of the most important features of this universal object is a possibility to display not only a static text, but expressions as well. At the same time, expressions can be located in the object together with a text. Let us examine a simple example of how it can be performed.

In the previous section, we have already made a report, which printed the "Hello, World" line and displayed a current date. To perform that, we had to allocate two objects in the report. Thus, one of them contained a greeting text, while the other one contained the "DATE" system variable. However, to display both a line and a date, we can use the "Text" object only. To accomplish this, we would need to put a line into the object, and this would look something like follows:

Hello, World! Today is [DATE].

Thus, when running the report, we can get something like follows:

Hello, World! Today is 01.01.2004.

What lead to such result? During FastReport report construction, it met in the text an expression enclosed in square brackets, calculated it and inserted the received value back into the text (having removed brackets, of course). The “Text” object can contain any number of expressions, together with a usual text. Both single variables and expressions can be enclosed in brackets (for example, [1+2*(3+4)]). Any constants, variables, functions, and DB fields can be used in expressions. We will observe these features later, futher in the chapter.

Thus, FastReport automatically recognizes expressions enclosed in square brackets in the text. But what should be done if our object contains square brackets, and we do not want them to be considered as expressions? For example, if we need to display such text as following:

a[1] := 10

FastReport considers [1] as an expression, and displays the following:

a1 := 10

that is not convenient to us, of course. One of the ways to avoid such situation is to disable the expression. Just disable the "AllowExpressions" property ("Allow Expressions" in the context menu), therefore all the expressions in the text will be ignored. In our example, FastReport would display exactly what we need:

a[1] := 10

Sometimes a text is required to contain both an expression and a text in square brackets, for example:

a[1] := [myVar]

Disabling of an expression allows to display square brackets in the required place, but it also disables handling of expression. In this case, FastReport allows to create another set of symbols, designating the expression. The "ExpressionDelimiters" object property, which is equal to "[,]" is responsible for it. In our case, the user can use angular brackets for the expressions, instead of square ones:

a[1] := <myVar>

At the same time, the "<,>" value must be set in the "ExpressionDelimiters" property. As you can see, the comma divides opening and closing symbols. There is one limitation however: the opening and closing symbols cannot be similar, so "%,%" will not work. One can set several symbols, for example "<%,%>" Thus, our example will look as follows:

a[1] := <%myVar%>